#include "downloadtable.h"
#include <QHBoxLayout>
#include <QSqlTableModel>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include "downloadtabledelegate.h"


DownloadTable::DownloadTable(QString dbTableName, QStringList dbTableColumn, QStringList dbTableColumnType,
                             QWidget *parent) :
    QWidget(parent)
{
    //dbTableName = "DownloadData";
    //dbTableColumn<<"Filename"<<"Url"<<"Location"<<"Percent"<<"Priority";
    //dbTableColumnType<<"TEXT"<<"TEXT"<<"TEXT"<<"INTEGER"<<"INTEGER";
    //dbTableColumnW<<30<<40<<20<<10;

    //QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //db.setDatabaseName("downloaddatabase.sqlite");

    /*if(!db.open()){
        QMessageBox *mb = new QMessageBox(QMessageBox::Information, "Attention!",
                                          "Last session data can not be loaded!", QMessageBox::Yes, this);
        mb->exec();
    }

    if(db.tables().size() == 0){
        QSqlQuery query(db);
        query.exec(getCreateDownloadTableQuery());
    }*/

    QSqlTableModel *model = new QSqlTableModel();
    model->setTable(dbTableName);
    model->select();
    model->setEditStrategy(QSqlTableModel::OnFieldChange);

    table = new QTableView(this);
    table->setModel(model);
    table->setSortingEnabled(true);
    table->setItemDelegate(new DownloadTableDelegate(this));

    QHBoxLayout *hLayout = new QHBoxLayout(this);
    hLayout->addWidget(table);
    setLayout(hLayout);
}

QString DownloadTable::getCreateDownloadTableQuery(){
    QString queryStr = "CREATE TABLE " + dbTableName +"(";
    for(int i = 0; i < dbTableColumn.size(); ++i){
        queryStr.append(dbTableColumn.at(i) + " " + dbTableColumnType.at(i));
        if(i < dbTableColumn.size()-1){
            queryStr.append(", ");
        }
    }
    queryStr.append(")");
    return queryStr;
}

/*QString DownloadTable::getCreateUrlTableQuery(){
    QString queryStr = "CREATE TABLE " + dbTableName + "(";
    for(int i = 0; i < dbTableColumn.size(); ++i){
        queryStr.append(dbTableColumn.at(i) + " " + dbTableColumnType.at(i));
        if(i < dbTableColumn.size()-1){
            queryStr.append(", ");
        }
    }
    queryStr.append(")");
    return queryStr;
}*/

QString DownloadTable::getInsertQuery(QString rename, QString savePath, double percent, int priority){
    QString queryStr = "INSERT INTO " + dbTableName + "(";
    for(int i = 0; i < dbTableColumn.size(); ++i){
        queryStr.append(dbTableColumn.at(i));
        if(i < dbTableColumn.size()-1){
            queryStr.append(", ");
        }
    }
    queryStr.append(") VALUES (" + rename + ", " + savePath + ", " +
                    QString::number(percent) + ", " + QString::number(priority));
    return queryStr;
}

int DownloadTable::rowCount(){
    QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
    return model->rowCount();
}

void DownloadTable::addRow(DownloadTask *task)
{
    QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
    int rowCount = model->rowCount();

    model->insertRow(rowCount);
    model->setData(model->index(rowCount, 0), task->rename());
    model->setData(model->index(rowCount, 1), task->path());
    model->setData(model->index(rowCount, 3), task->priority());
    model->submitAll();
    show();
}

int DownloadTable::progressForTask(int taskIndex){
    QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
    return model->index(taskIndex, 2).data().toInt();
}

int DownloadTable::priorityForTask(int taskIndex){
    QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
    return model->index(taskIndex, 3).data().toInt();
}

void DownloadTable::taskProgressChanged(QString filePath, int progress){
    QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model());
    int taskIndex = -1;
    int rowCount = model->rowCount();
    for(int i = 0; i < rowCount; ++i){
        QString str =  model->index(i, 1).data(Qt::DisplayRole).toString() +
                       model->index(i, 0).data(Qt::DisplayRole).toString();
        if(filePath == str){
            taskIndex = i;
            break;
        }
    }
    if(taskIndex > -1){
        model->setData(model->index(taskIndex, 2), progress);
        model->submitAll();
        show();
    }
}


